package com.ggh.jinjilive.ui.mall.shopping.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.LogUtils;
import com.bumptech.glide.Glide;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.ItemShopPingCarBinding;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.listener.AddOnClickListener;
import com.ggh.jinjilive.mobile.home.voice.VoiceHot;
import com.ggh.jinjilive.mobile.mall.goods.GoodsShopSPMobile;
import com.ggh.jinjilive.mobile.mall.shopping.ShopMessageMoblie;
import com.ggh.jinjilive.ui.home.voice.adapter.VoiceAdapter1;
import com.ggh.jinjilive.ui.home.voice.adapter.VoiceTypeAdapter;
import com.google.gson.Gson;

import java.util.List;

public class ShopingCarAdapter extends RecyclerView.Adapter<ShopingCarAdapter.ItemViewHolder> {


    private List<ShopMessageMoblie> shopMessageMoblieList;

    public void setAdd(List<ShopMessageMoblie> shopMessageMoblieList) {
        this.shopMessageMoblieList = shopMessageMoblieList;
        notifyDataSetChanged();
    }


    public void Add(List<ShopMessageMoblie> shopMessageMoblieList) {
        if(null != this.shopMessageMoblieList){
            this.shopMessageMoblieList.addAll(shopMessageMoblieList);
        }else {
            this.shopMessageMoblieList = shopMessageMoblieList;
        }
        notifyDataSetChanged();
    }


    /**
     * 更新数据
     */

    private AddItemOnClickListener addItemOnClickListener;
    public void setAddItemOnClickListener(AddItemOnClickListener addItemOnClickListener) {
        this.addItemOnClickListener = addItemOnClickListener;
    }


    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemShopPingCarBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_shop_ping_car, parent, false);
        return new ItemViewHolder(binding);
    }


    public void onBindViewHolder(@NonNull ItemViewHolder holder,final int position) {
        ShopMessageMoblie shopMessageMoblie = shopMessageMoblieList.get(position);
        if(null != shopMessageMoblie){
            //商品的适配器
            ShopingCarGoodsAdapter shopingCarGoodsAdapter  = new ShopingCarGoodsAdapter();
            //店铺图片
            Glide.with(holder.binding.storeImg).load(shopMessageMoblie.getImg()).into(holder.binding.storeImg);
            //店铺名称
            holder.binding.storeName.setText(shopMessageMoblie.getName());
            //是否选中
            if(shopMessageMoblieList.get(position).isSelected()){
                holder.binding.cbCheckall.setChecked(true);
            }else {
                holder.binding.cbCheckall.setChecked(false);
            }
            //CheckBox设置事件监听
            holder.binding.cbCheckall.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(holder.binding.cbCheckall.isChecked()){
                        shopMessageMoblieList.get(position).setSelected(true);
                        for (int i = 0; i < shopMessageMoblieList.get(position).getProduct().size();i++){
                            shopMessageMoblieList.get(position).getProduct().get(i).setSelected(true);
                        }
                    }else {
                        shopMessageMoblieList.get(position).setSelected(false);
                        for (int i = 0; i < shopMessageMoblieList.get(position).getProduct().size();i++){
                            shopMessageMoblieList.get(position).getProduct().get(i).setSelected(false);
                        }
                    }
                    shopingCarGoodsAdapter.notifyDataSetChanged();
                }
            });
            //店铺商品
            if(null != shopMessageMoblie.getProduct() && shopMessageMoblie.getProduct().size() > 0){
                shopingCarGoodsAdapter.setAddItemOnClickListener(new AddItemOnClickListener() {
                    @Override
                    public void onSkipClicked(int goodsPosition, Object mObject) {
                        List<GoodsShopSPMobile> goodsShopSPMobileList = (List<GoodsShopSPMobile>) mObject;
                        if(null != goodsShopSPMobileList && goodsShopSPMobileList.size() > 0){
                            boolean allSelected = true;
                            for (GoodsShopSPMobile goodsShopSPMobile : goodsShopSPMobileList){
                                if(!goodsShopSPMobile.isSelected()){
                                    allSelected = false;
                                    break;
                                }
                            }
                            holder.binding.cbCheckall.setChecked(allSelected);
                            shopMessageMoblieList.get(position).setSelected(allSelected);
                        }
                        if(null != addItemOnClickListener){
                            addItemOnClickListener.onSkipClicked(position,shopMessageMoblieList);
                        }
                    }
                });
                shopingCarGoodsAdapter.setAdd(shopMessageMoblieList.get(position).getProduct());
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(holder.binding.rvGoods.getContext(), LinearLayoutManager.VERTICAL, false) {
                    @Override
                    public boolean canScrollVertically() {
                        return false;
                    }
                };
                holder.binding.rvGoods.setLayoutManager(linearLayoutManager);
                holder.binding.rvGoods.setAdapter(shopingCarGoodsAdapter);

            }
        }
    }


    @Override
    public int getItemCount() {

        return null == shopMessageMoblieList ? 0 : shopMessageMoblieList.size();
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        ItemShopPingCarBinding binding;
        public ItemViewHolder(@NonNull ItemShopPingCarBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}
